﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
namespace YXQ.Dao
{

    public class CWDao
    {


        public CWDao()
        {

            Mapper.CreateMap<Y_CW, CWModel>();
        }
        public List<Y_CW> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_CW.Count();

                return edm.Y_CW.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public CWModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_CW.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_CW, CWModel>(query);
            }

        }

        public CWModel GetOneByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_CW.Where(s => s.ProjectId == id);

                return Mapper.Map<Y_CW, CWModel>(query.OrderByDescending(s => s.Id).FirstOrDefault());
            }
        }

        public List<CWModel> GetByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_CW.Where(s => s.ProjectId == id);



                return Mapper.Map<List<Y_CW>, List<CWModel>>(query.OrderByDescending(s => s.Id).ToList());
            }
        }

        public CWModel GetOneByProjectId(int id, int reid)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_CW.Where(s => s.ProjectId == id && s.Id == reid);



                return Mapper.Map<Y_CW, CWModel>(query.OrderByDescending(s => s.Id).FirstOrDefault());
            }
        }
        public int Save(CWModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_CW.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new Y_CW();

                    edm.AddToY_CW(query);

                }


                query.ProjectId = obj.ProjectId;

                query.gcyf = obj.gcyf;

                query.gcyt = obj.gcyt;

                query.sjyf = obj.sjyf;

                query.sjyt = obj.sjyt;

                query.jlzj = obj.jlzj;

                query.jlyf = obj.jlyf;

                query.jlyt = obj.jlyt;

                query.kcyf = obj.kcyf;

                query.gsyf = obj.gsyf;

                query.zjyf = obj.zjyf;

                query.dlyf = obj.dlyf;

                query.cgyf = obj.cgyf;

                query.qtfkdw = obj.qtfkdw;

                query.qthtje = obj.qthtje;

                query.qtfyms = obj.qtfyms;

                query.qtyf = obj.qtyf;

                query.createuserid = obj.createuserid;

                query.createrealname = obj.createrealname;

                query.createusername = obj.createusername;

                query.createtime = obj.createtime;

                edm.SaveChanges();

                return query.Id;


            }

        }


        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_CW.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

